package com.maiyuren.fishkit.dict

import com.maiyuren.fishkit.utils.FishAssetsFileUtils
import com.maiyuren.fishkit.utils.FishStorage
import org.json.JSONArray
import org.json.JSONObject

data class FishDictGroup(
    val id: String,
    val name: String,
    val items: MutableList<FishDict> = mutableListOf()
) {
    constructor(cfg: JSONObject) : this(
        id = cfg.getString("id"),
        name = cfg.getString("name")
    ) {
        val cfgItems = cfg.getJSONArray("items")
        for(i in 0 until cfgItems.length()) {
            val dictItem = cfgItems.getJSONObject(i)
            items.add(FishDict(FishDictCfg.fromJSON(dictItem)))
        }
    }
}

object FishDictMgr {
    val groups: MutableList<FishDictGroup> = mutableListOf();
    var defaultGroupIndex = 0;

    init {
        // 讀取RES配置
        val cfg = FishAssetsFileUtils.readJsonArrayFile("dict/index.json") as JSONArray;
        if (cfg != null) {
            for(i in 0 until cfg.length()) {
                groups.add(FishDictGroup(cfg.getJSONObject(i)))
            }
        }

        // todo:
        // 讀取用户配置，排序
        val sortCfg = FishStorage.getString(FishStorage.keys.dict_sort);

        // todo:
        // 添加的辞典
        val userImportDictCfg = FishStorage.getJson(FishStorage.keys.user_import_dicts);
    }



}